/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	Dotnetnuke PA module 
	Copyright (c) 2004, Oliver Hine (oliver.hine@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


************************************************************************************************/
using System;
using Votations.NSurvey.Emailing;
using Votations.NSurvey.BusinessRules;

namespace Votations.NSurvey.WebAdmin
{

	/// <summary>
	/// Handles asyncronous mailing
	/// </summary>
	public class ASyncMailing
	{
		private delegate void MailItDelegate(string sessionId, int surveyId, bool anonymousEntries, 
			string mailingEmails, EmailingMessage message);
		private MailItDelegate mailItDelegate;

		public ASyncMailing()
		{
			mailItDelegate = new MailItDelegate(this.MailIt);
		}

		public void MailIt(string sessionId, int surveyId, bool anonymousEntries, 
			string mailingEmails, EmailingMessage message)
		{
			IEmailing mailService = EmailingFactory.Create();
			string	Guid = null,
					taggedMessage = message.Body,
					email;
			string[] mailingList = mailingEmails.Split(',');

			SyncDataStore.SetRecords(sessionId + ":" + "Progress",0);

			try
			{
				// Send ther invitation email to the list
				for (int i=0; i<mailingList.Length; i++)
				{
					email = mailingList[i].Trim();
					Guid = new Voter().GenerateVoterInvitationUId(surveyId, email, anonymousEntries);
					message.Body = message.Body.Replace("[--invitationid-]", Guid);
					message.ToEmail = email;
					mailService.SendEmail(message);
					message.Body = taggedMessage;
					SyncDataStore.UpdateRecords(sessionId + ":" + "Progress", (i*100)/mailingList.Length);
				}
			}
			catch (Exception e)
			{ 
				SyncDataStore.SetRecords(sessionId + ":" + "Error", e);
			}
		}

		// Begins an asynchronous call
		public System.IAsyncResult BeginMailIt(string sessionId, int surveyId, bool anonymousEntries, 
			string mailingEmails, EmailingMessage message, System.AsyncCallback callback, object state)
		{
			return this.mailItDelegate.BeginInvoke(sessionId, surveyId, anonymousEntries, 
			mailingEmails, message, callback, state);
		}

		// Waits for the pending asynchronous request to complete.
		public void EndMailIt(System.IAsyncResult result)
		{
			this.mailItDelegate.EndInvoke(result);
		}
	}

}
